package com.example.quartzdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.quartzdemo.entity.QuartzTask;
import com.example.quartzdemo.entity.TaskLog;
import com.example.quartzdemo.entity.dto.QuartzTaskDto;
import com.example.quartzdemo.mapper.QuartzTaskMapper;
import com.example.quartzdemo.service.QuartzTaskRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
@Service
public class QuartzTaskRepositoryImpl extends ServiceImpl<QuartzTaskMapper, QuartzTask> implements QuartzTaskRepository  {
    @Override
    public Optional<QuartzTask> findByTaskId(String taskId) {
        Optional<QuartzTask> quartzTask = this.lambdaQuery().eq(QuartzTask::getTaskId, taskId).oneOpt();
        return quartzTask;
    }

    @Override
    public List<QuartzTask> findAll() {
        List<QuartzTask> quartzTasks = this.lambdaQuery().list();
        return quartzTasks;
    }

    @Override
    public void delete(QuartzTask task) {
        this.lambdaUpdate().eq(QuartzTask::getId, task.getId()).remove();
    }

    @Override
    public Page<QuartzTask> page(QuartzTaskDto dto,Integer pageNum,Integer sizeNum) {
        LambdaQueryWrapper<QuartzTask> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getTaskId())){
            lambdaQueryWrapper.like(QuartzTask::getTaskId,dto.getTaskId());
        }
        if(StringUtils.isNotBlank(dto.getClassName())){
            lambdaQueryWrapper.like(QuartzTask::getClassName,dto.getClassName());
        }
        if(StringUtils.isNotBlank(dto.getMethodName())){
            lambdaQueryWrapper.like(QuartzTask::getMethodName,dto.getMethodName());
        }
        if(StringUtils.isNotBlank(dto.getStatus())){
            lambdaQueryWrapper.like(QuartzTask::getStatus,dto.getStatus());
        }
        if(dto.getConcurrent()!=null){
            lambdaQueryWrapper.eq(QuartzTask::getConcurrent,dto.getConcurrent());
        }
        Page<QuartzTask> page=baseMapper.selectPage(new Page<>(pageNum,sizeNum),lambdaQueryWrapper);
        return page;
    }
}
